package com.ubxtech.i18n.config;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.config.ConfigService;
import com.ubxtech.i18n.properties.CustomI18nProperties;
import com.ubxtech.i18n.support.NacosStaticMessageSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author Victor.Xiao
 * @since 2022-11-16 20:36
 **/
@Configuration
@Slf4j
@EnableConfigurationProperties(CustomI18nProperties.class)
@ConditionalOnProperty(prefix = CustomI18nProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class CustomI18nConfig {

    @Bean
    public NacosStaticMessageSource nacosStaticMessageSource(CustomI18nProperties customI18nProperties,
                                                             NacosConfigProperties nacosConfigProperties,
                                                             NacosConfigManager nacosConfigManager) {
        ConfigService configService = nacosConfigManager.getConfigService();
        NacosStaticMessageSource nacosStaticMessageSource = new NacosStaticMessageSource(customI18nProperties, nacosConfigProperties, configService);
        nacosStaticMessageSource.initMessageSource();
        return nacosStaticMessageSource;
    }
}
